<template>
  <section id="position" >
    <div class="table">
      <table  >
        <thead>
          <tr>
            <th>时间</th>
            <th>产品</th>
            <th>手数 (方向)</th>
            <th>开仓价格</th>
            <th>当前买/卖价格</th>
            <th>盈亏</th>
            <th>止盈</th>
            <th>止损</th>
            <th>利息</th>
            <th>订单号</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody  v-if="maxSearchDataList&&maxSearchDataList.length">
          <tr  v-for="(item,index) in maxSearchDataList"  :key="index">
            <td>{{$js.timeToDate(item.create_time)}}</td>
            <td>{{item.goods_name}}</td>
            <td>{{item.num}}手 / {{item.buy_type==1?'买入':'卖出'}}</td>
            <td>{{item.price}}</td>
            <td :class="{red:item.zfType==1,green:item.zfType==2}">{{item.io_price}}</td>
            <td :class="item.yingkui>=0?'red':'green'">{{(item.yingkui>=0?'+':'')+item.yingkui}}</td>
            <td>{{item.profit_price}}</td>
            <td>{{item.loss_price}}</td>
            <td>{{item.interest}}</td>
            <td>{{item.order_id}}</td>
            <td>
              <span @click="alterChoose(12,index)">平仓</span>
              <span @click="alterChoose(13,index,true)">设置盈损</span>
              <span @click="goDetail(item.system_code,item)">图表</span>
            </td>
          </tr>
        </tbody>
       
    
      </table>
      <base-nodata v-if="!(maxSearchDataList&&maxSearchDataList.length)" ></base-nodata>
    </div>
     
     <base-alert :bol.sync="alertBol" :type.sync="alertType"  :pcData="maxSearchDataList[cPcDataIndex]" :pcNum="pcNum"  :zyVal="zyVal"
      :zsVal="zsVal" ></base-alert>
     
  </section>
</template>
<script>

export default {
  data() {
    return {
     alertBol:false,
     alertType:1,
     pcNum:0,
     cPcDataIndex:0,//平仓下标
     zyVal:"",//止盈值
     zsVal:"",//止损值
    }
  },
  computed:{
     //持仓最终数据
    maxCcDataList() {
      return this.$js.maxCcDataList();
    },
    maxSearchDataList(){
      var _data=this.maxCcDataList.data;
      var _searchDataVal=this.searchData;
      if(!_data)return false;
      var _searchData=[];
      if(_searchDataVal){
        for(var i=0;i<_data.length;i++){
        var _item=_data[i];
        var _bol=this.$js.dataSearch(_searchDataVal,_item);
         if(_bol)_searchData.push(_item);
        }
      }else{
        _searchData=_data;
      }
      return _searchData;
    }
  },
  methods:{
     //平仓、止盈止损弹窗
    alterChoose(type,index, bol) {
      this.cPcDataIndex = index;
      if (!bol) {
        this.pcNum = Number(this.maxSearchDataList[this.cPcDataIndex].num).toFixed(2);
      } else {
        this.zyVal = this.maxSearchDataList[this.cPcDataIndex].profit_price;
        this.zsVal = this.maxSearchDataList[this.cPcDataIndex].loss_price;
      }
       this.alertType = type;
       this.alertBol = true;
    },
      //获取小数位数
    getFloat(val, bol) {
      let _self = this;
      let _default = bol ? 0 : 1;

      let maxVal =
        String(val).split(".").length == 2
          ? val.split(".")[1].length
          : _default; // 最小变化单位
      return maxVal;
    },
    getKlineFloat(newsPrice){
        //取小数
        let  lent=this.getFloat(newsPrice)
        //小数列表
        let  floatList=[100,10,100,1000,10000,100000,1000000,10000];
        //列表长度
        let  _flentg=floatList.length-1;
        //小数大于列表长度侧默认小数位数
        lent> _flentg ? lent=floatList[_flentg]:lent=floatList[lent]
        //缓存小数信息
        localStorage.setItem('pricescale',lent)
        //修改小数引用对象
        this.$store.state.pricescale.num=lent;
     },
    //进入详情页
    goDetail(system_code,item){
      //改变交易页商品分类
      this.getKlineFloat(item.price);
      this.$store.commit('setdetialTab',item.type_id)
      this.$store.state.traViewSymboId=system_code;
      if(this.$route.name=='position'){
        //保存当前用户浏览的商品
         localStorage.setItem('symbol',system_code)
         this.$router.push("/trade");
      }else{
        //保存当前用户浏览的商品
        localStorage.setItem('symbol',system_code)
        //Tv事件
        const _store=this.$store.state.traViewEvent;
        //图表库更改商品
         _store.upSymbol(system_code)
       //点击图表回到顶部
         let num=10
         let distance=window.scrollY;
         let time=setInterval(()=>{
             num+=25
                 distance<= 0 ? clearInterval(time):
                 window.scrollTo(0,distance-=Math.random()*num)
            },10)
      }
    }
  },
  props:{
    searchData:{
      default:''
    }
  },
  created() {
   this.$store.getters.countCcDataList;
  },
  mounted(){
   
  }
}
</script>
